
options ls=100 ps=1000 nofmterr;

libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata';

data a0; set new.hedis2003;

    ***************************************************;
    **  Demographic Variables and Sample Selection   **;
    ***************************************************;

     if denom_flag=1;
     if state=' ' or state='00' or state > "53" or state in ("40","48") then delete;

     if age>=64; /* age 65 at end of year*/
 
     if plan_type='HMO - Health Maintenance Organization';

     if age>65 and totmonth<12 then delete; 

     deathyear=year(death_dt);
     deathmon =month(death_dt);

     if death1=1 and
       (deathyear=2003 or
       (deathyear=2004 and (deathmon>0 and deathmon<3)))
     then delete ;

     ***** Quality Rates *****;

     array rate bcs_d bcs_n ch_ace_d ch_acen1 cdc_d cdc_n1 cdc_n3 cdc_n4;  
     do over rate; 
        if rate=9 then rate=0; 
     end;

             *-- cleaning BCS --*;

     if sex=1 or age>69 then bcs_d=.;
     if bcs_d=. or bcs_d=0 then bcs_n=.; 

             *-- cleaning CMC --*;

     if ch_ace_d=0 then ch_acen1=.; 
 
             *-- cleaning CDC --*;

     if age>75 then cdc_d=.;
     if cdc_d=. or cdc_d=0 then do; cdc_n1=.; cdc_n3=.; cdc_n4=.; end;

             *-- Rename Variables and keep the same for all years --*;

     cmc_d = ch_ace_d;  label cmc_d = 'Denom for LDL-C Screening After AccuteCare';
     cmc_n1= ch_acen1;  label cmc_n1= 'Numer for LDL-C Screening After AccuteCare';


     *****  Frequent Selected Procedures  *****;

     array fps   fspcabgn fspptcan fsp_cc_n fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn
                 fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n ;
     do over fps; 
        if fps=999 then fps=0;  
     end;

     array uti   acd_n_t  acd_s_n  acd_m_n acdy_n_t acdy_s_n acdy_m_n  
                 ac_ov_n  ac_erv_n ac_asp_n ac_ors_n ;
     do over uti;
        if uti=999 then uti=0;
     end;

            *-- Gender specific Procedures --*;

     if sex=1 then do; fsp_ah_n=.; fsp_vh_n=.; end;  /** female only **/
     if sex=2 then do; fsp_p_n=.;              end;  /** male only **/


     ***** CAP measures  John and Bruce email 6-4-2010 *****;

     if fspcabgn>3 then fspcabgn=3;   
     if fspptcan>4 then fspptcan=4;   
     if fsp_cc_n>4 then fsp_cc_n=4;   
     
     array fps2 fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n;
     do over fps2; 
          if fps2>2 then fps2=2;  
     end;
       
     if acd_n_t > 12 then acd_n_t = 12;   * Total discharges set max 1 per month * ;    
     if acd_s_n > 12 then acd_s_n = 12;   * Surgery discharges *;
     if acd_m_n > 12 then acd_m_n = 12;   * Medical discharges *;

     if acdy_n_t > 365 then acdy_n_t = 365;  * Maximum possible in 12 months *;
     if acdy_s_n > 365 then acdy_s_n = 365;
     if acdy_m_n > 365 then acdy_m_n = 365;

     if ac_ov_n  > 50 then ac_ov_n  = 50; * Tot Out Patient visit set max 1 per week *;
     if ac_erv_n > 12 then ac_erv_n = 12; * Set max 1 per month *;
     if ac_asp_n > 12 then ac_asp_n = 12; 
     if ac_ors_n > 12 then ac_ors_n = 12;
 

     ********************************************************;
     ** Rename Variables and Keep consistent for All Years **;
     ********************************************************;

     n_cabg = fspcabgn ;  label n_cabg='CABG';
     n_ptca = fspptcan ;  label n_ptca='PTCA';
     n_cc   = fsp_cc_n ;  label n_cc  ='CardiacCathetherization';
     n_ce   = fsp_ce_n ;  label n_ce  ='CardiacEnarterectomy';
     n_rff  = fsp_rffn ;  label n_rff ='ReductionFemurFracture';
     n_thr  = fsp_thrn ;  label n_thr ='TotalHipReplacement';
     n_tkr  = fsp_tkrn ;  label n_tkr ='TotalKneereplacement';
     n_xli  = fsp_xlin ;  label n_xli ='ExcisionLargeIntest';
     n_oc   = fsp_oc_n ;  label n_oc  ='OpenCholecystectomy';
     n_clc  = fsp_clcn ;  label n_clc ='ClosedCholecstectomy';
     n_ah   = fsp_ah_n ;  label n_ah  ='AbdominalHysterectomy';
     n_vh   = fsp_vh_n ;  label n_vh  ='VaginalHysterectomy';
     n_p    = fsp_p_n ;   label n_p   ='Prostatectomy';

     iptotdis   = acd_n_t ;  label iptotdis ='InpatTotalDischarges';
     ipsurdis   = acd_s_n ;  label ipsurdis ='InpatSurgeryDischarges';
     ipmeddis   = acd_m_n ;  label ipmeddis ='InpatMedicineDischarges';

     iptotdays  = acdy_n_t ; label iptotdays ='InpatTotalDays';
     ipsurdays  = acdy_s_n ; label ipsurdays ='InpatSurgeryDays';
     ipmeddays  = acdy_m_n ; label ipmeddays ='InpatMedicineDays';

     amb_opv =  ac_ov_n ;   label amb_opv ='OutPatientVisits';
     amb_erv =  ac_erv_n ;  label amb_erv ='EmergencyRoomVisits';
     amb_asp =  ac_asp_n ;  label amb_asp ='AbmSugeryProcedures';
     amb_ors =  ac_ors_n ; label amb_ors ='ObservationRoomStays';

 data a; set a0;

     array items     bcs_d bcs_n cmc_d cmc_n1 cdc_d cdc_n1 cdc_n3 cdc_n4
                     n_cabg n_ptca n_cc n_ce n_rff n_thr n_tkr n_xli n_oc n_clc n_ah n_vh n_p
                     iptotdis ipsurdis ipmeddis iptotdays ipsurdays ipmeddays amb_opv amb_erv amb_asp amb_ors;
     do over items; if items>0 then items=1; end;

     %let all_vars= bcs_n    cmc_n1   cdc_n1   cdc_n3    cdc_n4 
                    n_cabg   n_ptca   n_cc     n_ce      n_rff    n_thr     
                    n_tkr    n_xli    n_oc     n_clc     n_ah     n_vh n_p
                    iptotdis ipsurdis ipmeddis iptotdays ipsurdays ipmeddays 
                    amb_opv  amb_erv  amb_asp  amb_ors;

     proc means data=a n nmiss mean nway;
               var &all_vars;
               output out=cr0 
                      mean=non0_bcs    non0_cmc   non0_cdc1   non0_cdc3   non0_cdc4
                           non0_cabg   non0_ptca  non0_cc     non0_ce     non0_rff    non0_thr     
                           non0_tkr    non0_xli   non0_oc     non0_clc    non0_ah     non0_vh   non0_p
                           non0_tdis   non0_sdis  non0_mdis   non0_tday   non0_sday   non0_mday 
                           non0_opv    non0_erv   non0_asp    non0_ors 
                      n=   totn_bcs    totn_cmc   totn_cdc1   totn_cdc3   totn_cdc4
                           totn_cabg   totn_ptca  totn_cc     totn_ce     totn_rff    totn_thr
                           totn_tkr    totn_xli   totn_oc     totn_clc    totn_ah     totn_vh   totn_p
                           totn_tdis   totn_sdis  totn_mdis   totn_tday   totn_sday   totn_mday
                           totn_opv    totn_erv   totn_asp    totn_ors ;



     proc means data=a mean n noprint nway;
               class org_id;
               var &all_vars;
               output out=cr1
                      mean=hnon0_bcs    hnon0_cmc   hnon0_cdc1   hnon0_cdc3   hnon0_cdc4
                           hnon0_cabg   hnon0_ptca  hnon0_cc     hnon0_ce     hnon0_rff    hnon0_thr
                           hnon0_tkr    hnon0_xli   hnon0_oc     hnon0_clc    hnon0_ah     hnon0_vh   
                           hnon0_p
                           hnon0_tdis   hnon0_sdis  hnon0_mdis   hnon0_tday   hnon0_sday   hnon0_mday
                           hnon0_opv    hnon0_erv   hnon0_asp    hnon0_ors
                      n=   hn_bcs    hn_cmc   hn_cdc1   hn_cdc3   hn_cdc4
                           hn_cabg   hn_ptca  hn_cc     hn_ce     hn_rff    hn_thr
                           hn_tkr    hn_xli   hn_oc     hn_clc    hn_ah     hn_vh     hn_p
                           hn_tdis   hn_sdis  hn_mdis   hn_tday   hn_sday   hn_mday
                           hn_opv    hn_erv   hn_asp    hn_ors;

 data bcr0; set cr0;
      cons=1;
      proc sort; by cons;

 data bcr1; set cr1;
      cons=1;
      proc sort; by cons;

data new.cr2003(drop=_FREQ_ _TYPE_ cons); merge bcr0 bcr1; by cons;
     proc print data=new.cr2003(obs=5);

endsas;